package main

import (
	"fmt"
	"math"
	"reflect"
	"runtime"
)

func eval(a, b int, op string) (int, error) {
	switch op {
	case "+":
		return a + b, nil
	case "-":
		return a - b, nil
	case "*":
		return a * b, nil
	case "/":
		q, _ := div(a, b)
		return q, nil
	default:
		//panic("unsupported operation:" + op)
		return 0, fmt.Errorf("unsupported operation: %s", op)
	}
}
func applyEval(op func(int, int) int, a, b int) int {
	//reflect:可以进行反射
	p := reflect.ValueOf(op).Pointer() //获得指针
	opName := runtime.FuncForPC(p).Name()
	fmt.Printf("Calling function %s with args"+"(%d,%d)\n", opName, a, b)
	return op(a, b)
}

func pow(a, b int) int {
	return int(math.Pow(float64(a), float64(b)))
}

//商和余数
//func div(a, b int) (int, int) {
//	return a / b, a % b
//}
// 另一种写法,函数体太长不建议此写法
func div(a, b int) (q, r int) {
	//return a / b, a % b
	q = a / b
	r = a % b
	return
}

func sum(numbers ...int) int {
	s := 0
	for i := range numbers {
		s += numbers[i]
	}
	return s
}

func main() {
	//fmt.Println(eval(3, 4, "x"))
	//if result, err := eval(3, 4, "x"); err != nil {
	//	fmt.Println("error:", err)
	//} else {
	//	fmt.Println(result)
	//}
	//
	//q, r := div(13, 3)
	//fmt.Println(q, "...", r)

	//fmt.Println(applyEval(pow, 3, 4))
	////匿名函数
	//fmt.Println(applyEval(func(a, b int) int {
	//	return int(math.Pow(float64(a), float64(b)))
	//}, 3, 4))

	fmt.Println(sum(1, 2, 3, 4, 5, 6))
}
